﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using xfsale.lib;
using xfsale.packet;

namespace xfsale.ui
{
    public partial class WinLogin : WinBase
    {
        public WinLogin()
        {
            InitializeComponent();
        }

        private void WinPasswod_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.Retry)
            {
                e.Cancel = true;
            }
        }

        /*private void HandleResponse(Pack response)
        {
            if (response.GetBaseByName("ReturnCode") == "0000")
            {
                App.Global.RegionCode = response.GetRecordByName("RegionCode");
                App.Global.BranchCode = response.GetRecordByName("BranchCode");
                App.Global.OpID = response.GetRecordByName("OpID");
                App.Global.OpName = response.GetRecordByName("OpName");
                App.Global.SysStatus = response.GetRecordByName("SysStatus");
                App.Global.SysWorkDay = response.GetRecordByName("SysWorkDay");
                App.Global.SysVersion = response.GetRecordByName("SysVersion");
                DialogResult = DialogResult.OK;
                return;
            }
            else
            {
                MessageBox.Show(response.GetBaseByName("ReturnMsg"));
                DialogResult = DialogResult.Retry;
            }
        }*/

        private void BtnOk_Click(object sender, EventArgs e)
        {
            string regionCode = System.Configuration.ConfigurationSettings.AppSettings["RegionCode"];
            string branchCode = System.Configuration.ConfigurationSettings.AppSettings["BranchCode"];
            string netCode = System.Configuration.ConfigurationSettings.AppSettings["NetCode"];
            string opCode = TxtOpCode.Text;
            string opPassword = TxtOpPassword.Text;

            if (!Regex.IsMatch(regionCode, @"^\d{4}$"))
            {
                SysMessage.Error("地区编号不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(branchCode, @"^\d{8}$"))
            {
                SysMessage.Error("网点号码不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(netCode, @"^\d{8}$"))
            {
                SysMessage.Error("销售网点号码不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(opCode, @"^\d{1,8}$"))
            {
                SysMessage.Error("用户帐号不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(opPassword, @"^\d{6}$"))
            {
                SysMessage.Error("用户密码不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            try
            {
                //填充请求数据
                UserLoginRequest request = new UserLoginRequest();
                request.serviceCode = "101801";
                request.regionCode = regionCode;
                request.branchCode = branchCode;
                request.netCode = netCode;
                request.opCode = opCode;
                request.opPassword = opPassword;

                //调用服务
                UserLoginResponse response = (UserLoginResponse)App.CallUserService("doUserLogin", request);

                //处理应答结果
                if (response.returnCode == "0000")
                {
                    //MessageBox.Show(response.returnMsg);
                    App.Global.RegionCode = request.regionCode;
                    App.Global.BranchCode = request.branchCode;
                    App.Global.NetCode = request.netCode;
                    App.Global.OpCode = request.opCode;
                    App.Global.OpName = response.opName;
                    DialogResult = DialogResult.OK;
                    return;
                }
                else
                {
                    MessageBox.Show(response.returnMsg);
                    DialogResult = DialogResult.Retry;
                    return;
                }
            }
            catch (Exception ex)
            {
                SysMessage.Error(ex.Message);
            }
        }
    }
}
